/**    
 * 文件名：DatabaseManage.java</br>
 *    
 * 版本信息：v1.0</br>
 * 日期：2017年7月10日</br>
 * © 2005-2017 雷技信息科技（上海）有限公司版权所有</br>
 *
 */
package com.logic.system;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Component;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;

/**
 *
 * 项目名称：system-server</br>
 * 类名称：DatabaseManage</br>
 * 类描述：</br>
 * 创建人：Aaron</br>
 * 创建时间：2017年7月10日 下午2:46:23</br>
 * 
 * @version 1.0
 *
 */
@Component
public class DatabaseManage
{
    @Autowired
    @Qualifier("writeSqlSessionFactory")
    private SqlSessionFactory sqlSessionWriteFactory;
    
    @Autowired
    private DataSourceTransactionManager txManager;
    
    private ThreadLocal<TransactionStatus> transactionStatus = new ThreadLocal<TransactionStatus>();
    
    public SqlSession startTrans(boolean isAutoCommit)
    {
        return sqlSessionWriteFactory.openSession(false);
    }
    
    public void insert(SqlSession sqlSession,String namespace, String methodname, Object parameter)
    {
        sqlSession.insert(namespace + "." + methodname, parameter);
    }
    
    public void commit(SqlSession sqlSession)
    {
        sqlSession.commit(true);
    }
    
    public void rollback(SqlSession sqlSession)
    {
        sqlSession.commit(false);
    }
    
    public void startTrans()
    {
        TransactionStatus status = transactionStatus.get();
        if (null == status)
        {
            DefaultTransactionDefinition def = new DefaultTransactionDefinition();
            // 事物隔离级别，开启新事务
            def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
            // 获得事务状态
            transactionStatus.set(txManager.getTransaction(def));
        }
    }
    
    public void commit()
    {
        TransactionStatus status = transactionStatus.get();
        if (null == status)
        {
            DefaultTransactionDefinition def = new DefaultTransactionDefinition();
            // 事物隔离级别，开启新事务
            def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
            // 获得事务状态
            transactionStatus.set(txManager.getTransaction(def));
        }
        
        txManager.commit(status);
    }
    
    public void rollBack()
    {
        TransactionStatus status = transactionStatus.get();
        if (null == status)
        {
            DefaultTransactionDefinition def = new DefaultTransactionDefinition();
            // 事物隔离级别，开启新事务
            def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
            // 获得事务状态
            transactionStatus.set(txManager.getTransaction(def));
        }
        
        txManager.rollback(status);
    }
}
